/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.daoImpl;

import br.com.deployment.dao.OptionsDAO;
import br.com.deployment.entities.Issues;
import br.com.deployment.entities.Options;
import br.com.deployment.entities.Response;
import br.com.deployment.genericdao.GenericDAO;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author ely
 */
@Stateless
@LocalBean
public class OptionsDAOImpl extends GenericDAO<Options> implements OptionsDAO{

    public OptionsDAOImpl(){
        super(Options.class);
    }
    
    @Override
    public List<Options> findOptions(Options options) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Options> findOptionsType(Issues issues) {
        
         String sql = "SELECT o FROM Issues i JOIN i.options o WHERE"
                + " i.id = :id and i.active = :active ORDER BY o.id ASC" ;
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", issues.getId());
        query.setParameter("active", true);
        return (List<Options>) query.getResultList();
    }
 
    @Override
    public List<Options> findOptionsResponse(Response response) {
        String sql = "SELECT o FROM Response r JOIN r.issues i JOIN r.optionses o WHERE"
                + " r.id = :id " ;
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", response.getId()); 
      
       
        return (List<Options>) query.getResultList();
    }
    
    public Long countAmountOptionsSelected(Options options){
        String sql = "SELECT COUNT(r) FROM QuestionnaireResponse qr JOIN qr.response r "
                + " JOIN r.issues i JOIN r.optionses o WHERE"
                + " o.id = :id and i.active = :active " ;
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", options.getId()); 
        query.setParameter("active", true); 
    
        System.err.println(query.getResultList());
        return (Long) query.getSingleResult();
    }
}
